package w4_c1_linkedlist;

import w3_c2_stack.ArrayStack;
import w3_c2_stack.Stack;

public class Main {
    /**
     * 测试给定Stack进行opCount次push和pop操作所需的时间，以秒计。
     * @param stack
     * @param opCount
     * @return
     */
    public static double testStack(Stack<Integer>stack, int opCount) {
        long startTime = System.nanoTime();
        int i;
        for(i = 0; i< opCount;i++)
            stack.push(i);
        for(i = 0; i< opCount;i++)
            stack.pop();
        long endTime = System.nanoTime();
        return (endTime - startTime) / 1e9;
    }

    /**
     * 测试LinkedListStack和ArrayStack性能差异。
     * @param args
     */
    public static void main(String[] args) {
        int [] opCounts = new int[] {100000, 10000000};
        Stack<Integer> [] stacks = new Stack[2];
        stacks[0] = new LinkedListStack<>();
        stacks[1]  = new ArrayStack<>();
        for (int opCount : opCounts) {
            System.out.println("\n-------" + "opCounts: " + opCount + "-------");
            for (Stack<Integer> stack : stacks) {
                double time = testStack(stack, opCount);
                System.out.println(String.format("%s: time = %f",stack.getName(),time));
            }
        }
    }
}
